package com.tdx.blog_nowcoder.service;

import com.tdx.blog_nowcoder.constant.CommentReplyConstant;
import com.tdx.blog_nowcoder.dao.CommentMapper;
import com.tdx.blog_nowcoder.entity.Comment;
import com.tdx.blog_nowcoder.entity.Message;
import com.tdx.blog_nowcoder.utils.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private DiscussPortService discussPortService;

    public List<Comment> selectCommentsByEntity(int entity_type, int entity_id, int offset, int limit){
        return commentMapper.selectCommentsByEntity(entity_type,entity_id,offset,limit);
    }
    public int selectCountByEntity(int entity_type,int entity_id){
        return commentMapper.selectCountByEntity(entity_type,entity_id);
    }
    public int insertComment(Comment comment){

        if (comment == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }

        // 添加评论
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        int rows = commentMapper.insertComment(comment);

        // 更新帖子评论数量
        if (comment.getEntityType() == CommentReplyConstant.ENTITY_TYPE_COMMENT) {
            int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
            discussPortService.updateCommentCount(comment.getEntityId(), count);
        }
        return rows;
    }

    public Comment findCommentById(int comment_id) {
        return commentMapper.findCommentById(comment_id);
    }






}
